import { isEmptyData } from "./tools"

function IsPtInPoly(aLat, aLon, pointList) {
    /*
    :param aLon: double 经度
    :param aLat: double 纬度
    :param pointList: list [{latitude: 22.22, longitude: 113.113}...] 多边形点的顺序需根据顺时针或逆时针，不能乱
    */
    let iSum = 0
    let iCount = pointList.length


    if (iCount < 3) {
        return false
    }
    //  待判断的点(x, y) 为已知值
    let y = aLat
    let x = aLon
    for (let i = 0; i < iCount; i++) {
        let y1 = pointList[i].latitude
        let x1 = pointList[i].longitude
        let y2 = null
        let x2 = null
        if (i === iCount - 1) {
            y2 = pointList[0].latitude
            x2 = pointList[0].longitude
        } else {
            y2 = pointList[i + 1].latitude
            x2 = pointList[i + 1].longitude
        }
        // 当前边的 2 个端点分别为 已知值(x1, y1), (x2, y2)
        if (((y >= y1) && (y < y2)) || ((y >= y2) && (y < y1))) {
            //  y 界于 y1 和 y2 之间
            //  假设过待判断点(x, y)的水平直线和当前边的交点为(x_intersect, y_intersect)，有y_intersect = y
            // 则有（2个相似三角形，公用顶角，宽/宽 = 高/高）：|x1 - x2| / |x1 - x_intersect| = |y1 - y2| / |y1 - y|
            if (Math.abs(y1 - y2) > 0) {
                let xIntersect = x1 - ((x1 - x2) * (y1 - y)) / (y1 - y2)
                if (xIntersect < x) {
                    iSum += 1
                }
            }
        }
    }
    if (iSum % 2 !== 0) {
        return true
    } else {
        return false
    }
}

export function getPtInPoly(aLat, aLon, polygons) {
    return new Promise((resolve, reject) => {
        let polyData = null
        polygons.forEach(data => {
            let poly = IsPtInPoly(aLat, aLon, data.points)
            if (poly) {
                polyData = data
            }
        })
        if (isEmptyData(polyData)) {
            resolve(polygons[0])
        } else {
            resolve(polyData)
        }
    });
}

export function getAllPolygonsData(list) {
    return new Promise((resolve, reject) => {
        let results = list.filter(data => data.id !== -1).map(data => {
            return data.gps?.map(gpsArray => {
                let gps = gpsArray.geometry?.coordinates?.map(coordinates => {
                    return coordinates.map(item => {
                        let { lng, lat } = bMapTransqqMap(item[0], item[1]) as {
                            lng: number,
                            lat: number,
                        }
                        return {
                            latitude: item[1],
                            longitude: item[0]
                        }
                    })
                }).flat(Infinity)
                return {
                    points: gps,
                    fillColor: '#C2E9F8B3',
                    strokeWidth: 3,
                    strokeColor: '#00C8EC',
                    level: 'abovebuildings',
                    region_id: data.id,
                    region_name: data.region_name,
                }
            })
        }).flat(Infinity)
        resolve(results)
    })
}
export function bMapTransqqMap(lng, lat) {
    if (lng == '' && lat == '') {
        return {
            lng: '',
            lat: ''
        }
    }
    let x_pi = (3.14159265358979324 * 3000.0) / 180.0;
    let x = lng - 0.0065;
    let y = lat - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);
    return {
        lng: lngs,
        lat: lats,

    }
}
